package com.genokon.config;

import com.genokon.util.StringToDateConverter;
import com.genokon.util.StringToListConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author by baichaoda
 * @date 2020/11/4 9:40
 */
@Configuration
public class SpringDataConvert {
    @Resource
    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    @PostConstruct
    public void addConversionConfig() {
        ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) requestMappingHandlerAdapter.getWebBindingInitializer();
        assert initializer != null;
        if (initializer.getConversionService() != null) {
            GenericConversionService genericConversionService = (GenericConversionService)initializer.getConversionService();
            //添加字符串转换为list集合的转换机器
            genericConversionService.addConverter(new StringToListConverter());
            //添加字符串转换为日期类型的字符串
            genericConversionService.addConverter(new StringToDateConverter());

        }
    }
}
